package mobidever.mygodutch.activity;

import mobidever.mygodutch.R;
import mobidever.mygodutch.activity.base.ActivityFrame;
import mobidever.mygodutch.adapter.AdapterAccountBookSelect;
import mobidever.mygodutch.adapter.AdapterPayout;
import mobidever.mygodutch.bussiness.BussinessAccountBook;
import mobidever.mygodutch.bussiness.BussinessPayout;
import mobidever.mygodutch.controls.SliderItem;
import mobidever.mygodutch.controls.SliderMenu.OnSliderMenuClickListener;
import mobidever.mygodutch.model.ModelAccountBook;
import mobidever.mygodutch.model.ModelPayout;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.AdapterView.AdapterContextMenuInfo;

public class ActivityPayout extends ActivityFrame implements
		OnSliderMenuClickListener
{

	private ListView mPayoutList;
	private AdapterPayout mAdapterPayout;
	private BussinessPayout mBussinessPayout;
	private ModelPayout mSelectModelPayout;
	private ModelAccountBook mSelectAccountBook;
	private BussinessAccountBook mBussinessAccountBook;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);

		AppendMainBody(R.layout.payout);
		initVariable();
		initView();
		bindData();
		initListener();
		createSlideMenu(R.array.SlideMenuPayout);
	}

	private void initVariable()
	{
		mBussinessPayout = new BussinessPayout(this);
		mBussinessAccountBook = new BussinessAccountBook(this);
		mSelectAccountBook = mBussinessAccountBook.getDefaultAccountBook();
	}

	private void initView()
	{
		mPayoutList = (ListView) findViewById(R.id.lvPayout);
	}

	private void initListener()
	{
		registerForContextMenu(mPayoutList);
	}

	private void bindData()
	{
		mAdapterPayout = new AdapterPayout(this,(int)(mSelectAccountBook.getAccountBookID()));
		mPayoutList.setAdapter(mAdapterPayout);
	}

	private boolean deletePayout()
	{
		return mBussinessPayout.deleteModelPayoutByID((int)mSelectModelPayout.getPayoutID());
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo)
	{
		AdapterContextMenuInfo _AdapterContextMenuInfo = (AdapterContextMenuInfo) menuInfo;
		mSelectModelPayout = (ModelPayout) mPayoutList.getAdapter().getItem(
				_AdapterContextMenuInfo.position);

		menu.setHeaderIcon(R.drawable.payout_small_icon);
		menu.setHeaderTitle(mSelectModelPayout.getCategoryName());

		creatMenu(menu);
	}

	@Override
	public boolean onContextItemSelected(MenuItem item)
	{
		switch (item.getItemId())
		{
		case 1:
			Intent _Intent = new Intent(ActivityPayout.this,ActivityPayoutAddOrEdit.class);
			_Intent.putExtra("ModelPayout", mSelectModelPayout);
			startActivityForResult(_Intent, 1);
			break;
		case 2:
			String _Title = getString(R.string.DialogMessagePayoutDelete,
					new Object[]
					{ mSelectModelPayout.getCategoryName()});
			AlertDailogDeleteOrCanle(_Title, new OnEditOrDeleteListner());
			break;
		default:
			break;
		}
		return super.onContextItemSelected(item);
	}

	public void onSliderMenuClick(View pView, SliderItem pSliderItem)
	{
		// showMsg(pSliderItem.getItemName());
		if (pSliderItem.getItemID() == 0)
		{
			showAccountBookSelectDialog();
			SlideMenuToggle();
		}
	}

	public void showAccountBookSelectDialog()
	{
		View _View = LayoutInflater.from(this).inflate(R.layout.dialog_list, null);
		ListView _ListView = (ListView) _View.findViewById(R.id.lvSelect);
		AdapterAccountBookSelect _AdapterAccountBookSelect = new AdapterAccountBookSelect(null, this);
		_ListView.setAdapter(_AdapterAccountBookSelect);

		AlertDialog.Builder _Builder = new AlertDialog.Builder(this);
		_Builder.setIcon(R.drawable.payout_small_icon)
				.setTitle(R.string.ButtonTextSelectAccountBook)
				.setView(_View)
				.setNegativeButton(getString(R.string.ButtonTextBack), null)
				;
		AlertDialog _AlertDialog = _Builder.create();
		_AlertDialog.show();
		_ListView.setOnItemClickListener(new OnAccountBookItemClickListener(_AlertDialog));
	}

	private class OnAccountBookItemClickListener implements OnItemClickListener
	{
		
		AlertDialog _AlertDialog;
		
		public OnAccountBookItemClickListener(AlertDialog pAlertDialog)
		{
			_AlertDialog = pAlertDialog;
		}

		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3)
		{
			mSelectAccountBook = (ModelAccountBook) arg0.getAdapter().getItem(arg2);
			bindData();
			_AlertDialog.dismiss();
		}
		
	}
	
	private class OnEditOrDeleteListner implements
			DialogInterface.OnClickListener
	{

		public void onClick(DialogInterface dialog, int which)
		{
			boolean _Result = deletePayout();
			if (_Result)
			{
				bindData();
				showMsg(getString(R.string.TipsDeleteSeucceed));
			} else
			{
				showMsg(getString(R.string.TipsDeleteFail));
			}
		}
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{

		super.onActivityResult(requestCode, resultCode, data);
		bindData();
	}

}
